package it.andreaartuso.paletta.activity;

import it.andreaartuso.paletta.R;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

public class PalettaActivity extends Activity{

	MediaPlayer mp;
	
	private static final int CAMERA_PIC_REQUEST = 1337;  

	 /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.paletta);
        overridePendingTransition(R.anim.slide_up, R.anim.slide_down);
        TextView paletta_num = (TextView)this.findViewById(R.id.paletta_num);
        Bundle b = getIntent().getExtras();
        String value = b.getInt("voto") + "";
        if(b.getInt("voto")==0){
        	mp = MediaPlayer.create(PalettaActivity.this,R.raw.lvl_0);
            mp.start();
        } else if(b.getInt("voto")==10) {
        	mp = MediaPlayer.create(PalettaActivity.this,R.raw.lvl_10);
            mp.start();
        }

        paletta_num.setText(value);
        ImageButton btn_back = (ImageButton)this.findViewById(R.id.btn_h_back);
        btn_back.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) { 
				onKeyDown(KeyEvent.KEYCODE_BACK, new KeyEvent(0, 0,
					    KeyEvent.ACTION_DOWN, 0, 0, 0, 0, 0,
					    KeyEvent.FLAG_SOFT_KEYBOARD|KeyEvent.FLAG_KEEP_TOUCH_MODE));
				return true;
			}
		});
        final ImageButton btn_camera = (ImageButton)this.findViewById(R.id.btn_h_camera);
        btn_camera.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if(event.getAction() == MotionEvent.ACTION_DOWN){
					if (btn_camera.isSelected()){
						btn_camera.setSelected(false);
			        } else {
			        	btn_camera.setSelected(true);
			        }
					Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
					startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
				}
				return true;
			}
		});
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        if (requestCode == CAMERA_PIC_REQUEST) {  
        	Intent openHistoryActivityIntent = new Intent("it.andreaartuso.paletta.activity.HISTORYACTIVITY");
			startActivity(openHistoryActivityIntent);
        }  
    }

    @Override
    public void onPause()
    {
      super.onPause();
      if(mp!=null){
    	  mp.release();
      }
      System.gc();
    }

    public void onDestroy()
    {
      super.onDestroy();
      System.gc();
    }

    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
    	if (keyCode == KeyEvent.KEYCODE_BACK) { 
    		finish();	 
    		overridePendingTransition(R.anim.no_anim,R.anim.slide_down); 
    		return true; 
    	}
    	return super.onKeyDown(keyCode, event); 
    }
}